// pages/quick_bind_phone/quick_bind_phone.js
const WxParse = require('../../lib/wxParse/wxParse.js');
const config = require('../../config.js');
const request = require('../../utils/request.js')
const utils = require('../../utils/util.js');
import {
    bindPhone,
    getWxDecodePhone
} from '../../static/api/api.js'
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        params: null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let that = this;
        this.setData({
            params: options
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },
    getPhoneNumber: function(ec) {
        let that = this;
        console.log(ec)
        wx.showLoading({
            title: '加载中...',
        })
        wx.login({
            success: function(ev) {
                console.log('登录成功')
                console.log(ev)
                let params={
                    code:ev.code,
                    type:2,
                    encryptedData: ec.detail.encryptedData,
                    iv: ec.detail.iv
                }
                getWxDecodePhone(params).then(response=>{
                    wx.hideLoading()
                    
                    let phone = response.data.phoneNumber;
                    that.bindPhone({
                        phone: phone,
                        type:2,
                    })
                    
                    
                    
                }).catch(response=>{
                    wx.hideLoading()
                })
            }
        })
    },
    bindPhone(cparams){
        wx.showLoading({
            title: '加载中...',
        })
        bindPhone(cparams).then(response => {
            wx.hideLoading()
            wx.showToast({
                icon: 'none',
                title: '绑定成功',
            })
            wx.setStorageSync('token', response.data.session_key)
            wx.setStorageSync('unionid', response.data.unionid)
            setTimeout(function () {
                wx.navigateBack({
                    delta: 2,
                })
            }, 500)
        }).catch(response => {
            wx.hideLoading()
        })
    }


})